#include "aprsgraphicsscene.h"

aprsGraphicsScene::aprsGraphicsScene(QObject *parent)
    :QGraphicsScene(parent)
{

}

void aprsGraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    QGraphicsScene::mousePressEvent(event); //Call the ancestor
    if(event->button() == Qt::RightButton)
    {
        QPointF locationOfMouse = event->scenePos();
        foreach(basicStation* station, qlist)
        {
            qreal qX = station->pixmapItem->x();
            qreal qY = station->pixmapItem->y();
            qreal lowValueX = locationOfMouse.x() - 10;
            qreal highValueX = locationOfMouse.x() + 10;
            qreal lowValueY = locationOfMouse.y() - 10;
            qreal highValueY = locationOfMouse.y() + 10;
            if(((station->pixmapItem->x() >= lowValueX) && (station->pixmapItem->x() <= highValueX)) &&
                ((station->pixmapItem->y() >= lowValueY) && (station->pixmapItem->y() <= highValueY)))
            {
                //QPointF stationXY(station->pixmapItem->x(), station->pixmapItem->y());
                QPointF stationLatLon = station->LonLat;
                QString sLatLon = QString::number(stationLatLon.x(), 'g', 6) + ", " + QString::number(stationLatLon.y(), 'g', 6);

                QMessageBox msgBox;
                //Set title
                msgBox.setWindowTitle(station->callsign + " Information");
                //Set text
                msgBox.setText(station->callsign);
                //Set predefined icon, icon is show on left side of text.
                msgBox.setIcon(QMessageBox::Information);
                //set inforative text
                msgBox.setInformativeText("(Longitude, Latitude): (" + sLatLon + ")");
                //Add ok and cancel button.
                msgBox.setStandardButtons(QMessageBox::Ok);
                //Set focus of ok button
                msgBox.setDefaultButton(QMessageBox::Ok);
                msgBox.exec();
            }
        }
    }
}

QPointF aprsGraphicsScene::ConvertPixToLatLon(QPointF *latlon)
{
    //Map is 3600 X 1800.  Each 10 pixels horizontal is a new longitude. each 10 pixels vertical is a new latitude
    //This is assuming that at the exact center of the map is the true lat-lon point (0,0)
    //Each Icon is 20x20 so to draw directly on top of each point you must offset each axis by 10.
    qreal x = ((latlon->x() * 10) + 1800 - 10); //Plus half the map size for offset.
    qreal y = ((latlon->y() * -10) + 900 - 10);  //Again offset for map size. Multiply by -10 since the pixel's grow downward

    QPointF pixCoords;
    pixCoords.setX(x);
    pixCoords.setY(y);

    return pixCoords;
}
